<template>
	<div class="page-wrapper">
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.strator_account" placeholder="账号" clearable style="width: 200px;"></el-input>
			</el-form-item>
			<el-form-item>
				<el-input v-model="state.queryForm.strator_mobile" placeholder="手机号" clearable style="width: 200px;"></el-input>
			</el-form-item>
			<el-form-item>
				<el-input v-model="state.queryForm.strator_email" placeholder="邮箱" clearable style="width: 200px;"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()" type="primary">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button @click="clearForm()">重置</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="addOrUpdateHandle()" v-if="hasRule('system/user/index/add')">新增</el-button>
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" width="100%" height="100%">
            <!-- @selection-change="selectionChangeHandle" -->
			<!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
			<el-table-column prop="strator_account" label="账号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="strator_nickname" label="头像/昵称" header-align="center" align="center">
                <template #default="scope">
					<div class="w100 flex flexL flexC">
                        <div v-if="scope.row.strator_avatar" class="mr10">
                            <el-image
                                style="width: 30px; height: 30px;border-radius: 50%;"
                                :src="scope.row.strator_avatar"
                                :zoom-rate="1"
                                :max-scale="7"
                                :z-index="1001"
                                :min-scale="0.2"
                                :preview-teleported="true"
                                :preview-src-list="[scope.row.strator_avatar]"
                                :initial-index="0"
                                fit="cover"
                                />
                        </div>
                        <div>{{ scope.row.strator_nickname }}</div>
                    </div>
				</template>
            </el-table-column>
			<!-- <el-table-column prop="realName" label="姓名" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="strator_mobile" label="手机号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="strator_email" label="邮箱" header-align="center" align="center"></el-table-column>
			<el-table-column prop="department.department_name" label="部门" header-align="center" align="center"></el-table-column>
			<el-table-column prop="positions" label="职务" header-align="center" align="center">
                <template #default="scope">
                    <div class="w100" v-if="scope.row.is_super == 1">
                        超级管理员
                    </div>
					<div class="w100" v-else-if="scope.row.positions.length">
                        <span v-for="(item,i) in scope.row.positions">{{ item.position_name }}{{ i != scope.row.positions.length-1 ? '、' : '' }}</span>
                    </div>
				</template>
            </el-table-column>
			<el-table-column prop="created_at" label="创建时间" header-align="center" align="center" width="180"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/update')">修改</el-button>
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id, 1)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/setpost')">设置职务</el-button>
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id, 4)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/getpost')">查看职务权限</el-button>
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id, 2)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/setrole')">设置权限</el-button>
					<el-button type="danger" link @click="addOrUpdateHandle(scope.row.id, 3)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/pass')">重置密码</el-button>
					<el-button type="danger" link @click="deleteHandle(scope.row.id)" v-if="scope.row.is_super != 1 && hasRule('system/user/index/delete')">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.per_page"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
	</div>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import AddOrUpdate from './add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'
import { hasRule } from '@/utils/tool'

const state: IHooksOptions = reactive({
	dataListUrl: '/api/strator/list',
	deleteUrl: '/api/strator/delete',
    isPage: true,
	queryForm: {
		strator_account: '',
		strator_mobile: '',
		strator_email: ''
	}
})
const clearForm = () => {
    state.queryForm.strator_account = '';
    state.queryForm.strator_mobile = '';
    state.queryForm.strator_email = '';
    getDataList();
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number, type?: any) => {
	addOrUpdateRef.value.init(id, type)
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteHandle } = useCrud(state)
</script>
